﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using FunstreamTV.Model;
using FunstreamTV.Model.User;
using FunstreamTV.Tools;

namespace FunstreamTV.ViewModel
{
    public class MainViewModel : BaseViewModel
    {
        private string password;
        private string login;

        public string Login
        {
            get { return login; }
            set { login = value; OnPropertyChanged(); }
        }

        public string Password
        {
            get { return password; }
            set { password = value; OnPropertyChanged(); }
        }

        public MainViewModel()
        {
            //Login = "funstreamTest";
            //Password = "qwe123";
        }

        public async Task<Response<bool>> Authorize()
        {
            try
            {
                var requester = new Requester();
                var response = await requester.Post<LoginResponse, Login>("/user/login", new Login
                {
                    name = Login,
                    password = Password
                });
                if (response.Item != null)
                {
                    await FileManager.Save(response.Item, "LoginResponse");
                    return new Response<bool> {Item = true};
                }

                await new MessageDialog(response.Message).ShowAsync();
                return new Response<bool> {Item = false, Message = response.Message};
            }
            catch (Exception e)
            {
                await new MessageDialog(e.Message).ShowAsync();
                return new Response<bool> {Item = false, Message = e.Message};
            }
        }

        public async Task<bool> Load()
        {
            try
            {
                await GetUser();
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return false;
            }

        }
    }
}
